<?php
require_once 'models/battles.php';
require_once 'dbhelper.php';

class battles_controller extends dbhelper {
    public function getAllBattles()
    {
        $battles = array();
        $sql = "SELECT * FROM battles";
        if(isset($_SESSION['search']) && $_SESSION['search'] != ''){
            $sql .=" where title like '%".$_SESSION['search']."%' or sub_title like '%".$_SESSION['search']."%'";
        }
        $sql .= " order by title asc ";
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $battle = new battles($row["id"],$row["title"],$row["sub_title"],$row["type"],$row["running"],$row["max_vote"],$row["vote_start_from"],$row["date_created"],$row["last_update"],$row["category"]);
                $battles[count($battles)] = $battle;
                
            }
            $_SESSION['search'] = '';
        return $battles;
    }
    public function getLastBattles()
    {
        $battles = array();
        $sql = "SELECT * FROM battles";
        $sql .= " order by date_created desc LIMIT 0,3 ";
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $battle = new battles($row["id"],$row["title"],$row["sub_title"],$row["type"],$row["running"],$row["max_vote"],$row["vote_start_from"],$row["date_created"],$row["last_update"],$row["category"]);
                $battles[count($battles)] = $battle;
                
            }
        return $battles;
    }
    
    public function getBattlesById($id)
    {
        $sql = "SELECT * from battles where id = '$id' ";
        $result = parent::execQuery($sql);
        if($result && parent::getNumRows($result))
        {
            $row = parent::fetchArray($result);
            $battle = new battles($row["id"],$row["title"],$row["sub_title"],$row["type"],$row["running"],$row["max_vote"],$row["vote_start_from"],$row["date_created"],$row["last_update"],$row["category"]);   
        }
        return $battle;
    }
    public function deleteBattlesbyId($id)
    {
        $sql = "DELETE FROM battles WHERE id = '$id'";
        $sql_i = "DELETE FROM battle_items WHERE battle_id = '$id'";
        parent::execQuery($sql);
        parent::execQuery($sql_i);
        if (parent::getAffectedRows() > 0){
            return true;
            }
        return false;
    }
    public function addBattles($battle) {
        $sql = "INSERT INTO battles(title,sub_title,type,running,max_vote,vote_start_from,date_created,last_update) VALUES('$battle->title', '$battle->sub_title', '$battle->type', '$battle->running', '$battle->max_vote', '$battle->vote_start_from', '$battle->date_created', '$battle->last_update','$battle->category')";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function editBattles($battle) {
        $sql = "UPDATE battles SET title = '$battle->title', sub_title = '$battle->sub_title', running = '$battle->running', max_vote = '$battle->max_vote', vote_start_from = '$battle->vote_start_from', last_update = '$battle->last_update', category = '$battle->category'";
        $sql = $sql . " WHERE id = '$battle->id'";
        //var_dump($sql);exit;
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function pagging()
    {
        $sql = "SELECT * FROM battles";
        $result = parent::execQuery($sql);
        $row = parent::getAffectedRows($result);
        return $row;
    }
    
    function totalVotes($battleID)
    {
	$sql = "select sum(votes) as TotalVotes from battle_items where battle_id='".$battleID."'";
	$rs = mysql_query($sql) or die("Cannot Execute Query : ".$sql."<P>".mysql_error());
	$row = mysql_fetch_array($rs);
	return $row['TotalVotes'];
    }
    
    public function pagingBattle($startrecord,$r) {
		$battleArr = array();
		$startrecord *= $r; 
            $sql = "SELECT * FROM battles";
            if(isset($_SESSION['search']) && $_SESSION['search'] != ''){
            $sql .=" where title like '%".$_SESSION['search']."%' or sub_title like '%".$_SESSION['search']."%'";
            }
            $sql .= " order by title asc ";
            $sql .= " LIMIT $startrecord,$r";
            $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $battles_p = new battles($row["id"],$row["title"],$row["sub_title"],$row["type"],$row["running"],$row["max_vote"],$row["vote_start_from"],$row["date_created"],$row["last_update"],$row["category"]);
                $battleArr[count($battleArr)] = $battles_p;
            }
            
        return $battleArr;
	}
    
	public function countBattle() {
		$sql = "SELECT * FROM battles";
		parent::execQuery($sql);
		$result = parent::getAffectedRows();      
		return $result;
	}
	public function countPageBattle() {
		$sql = "SELECT * FROM battles";
        if(isset($_SESSION['search']) && $_SESSION['search'] != ''){
            $sql .=" where title like '%".$_SESSION['search']."%' or sub_title like '%".$_SESSION['search']."%'";
            }
        $sql .= " order by title asc ";
		parent::execQuery($sql);
		$result = parent::getAffectedRows();      
		return $result;
	}
    function validateStartVote($info){
		//if it's NOT valid
		if(strlen($info) < 2 && ctype_digit($info))
			return true;
		//if it's valid
		else
			return false;
	}
    function validateMaxVote($info){
		//if it's NOT valid
		if(strlen($info) < 5 && ctype_digit($info))
			return true;
		//if it's valid
		else
			return false;
	}
    function validateTitle($title){
		//if it's NOT valid
		if(strlen($title) < 4)
			return false;
		//if it's valid
		else
			return true;
	}
}
?>
